package com.kawa.kemanage.controller.api;

import com.kawa.kemanage.entity.Advertiser;
import com.kawa.kemanage.service.AdvertiserService;
import com.kawa.kemanage.util.Result;
import com.kawa.kemanage.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 广告主管理控制器
 */
@RestController
@RequestMapping("/api/advertisers")
public class AdvertiserController {
    @Autowired
    private AdvertiserService advertiserService;

    /**
     * 查询广告主列表（分页）
     * @param page 当前页码
     * @param size 每页显示条数
     * @return 分页结果
     */
    @GetMapping
    public Result<PageResult<Advertiser>> findAll(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        return Result.success(advertiserService.findByPage(page, size));
    }

    /**
     * 根据ID查询广告主信息
     * @param id 广告主ID
     * @return 广告主信息
     */
    @GetMapping("/{id}")
    public Result<Advertiser> findById(@PathVariable Integer id) {
        return Result.success(advertiserService.findById(id));
    }

    /**
     * 创建新的广告主
     * @param advertiser 广告主信息
     * @return 创建后的广告主信息
     */
    @PostMapping
    public Result<Advertiser> create(@RequestBody Advertiser advertiser) {
        return Result.success(advertiserService.create(advertiser));
    }

    /**
     * 更新广告主信息
     * @param id 广告主ID
     * @param advertiser 更新的广告主信息
     * @return 更新后的广告主信息
     */
    @PutMapping("/{id}")
    public Result<Advertiser> update(@PathVariable Integer id, @RequestBody Advertiser advertiser) {
        return Result.success(advertiserService.update(id, advertiser));
    }

    /**
     * 删除广告主
     * @param id 广告主ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Integer id) {
        advertiserService.delete(id);
        return Result.success();
    }
}
